﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;

namespace CarInfor.DAL
{
    public class MsgInterface
    {
        string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + MyGlobal.SmsPath + "smsdb.mdb;jet OleDB:Database Password=2011";
        /// <summary>
        /// 电话号码
        /// </summary>
        string telephone = "";
        public string Telephone
        {
            get { return telephone; }
            set { telephone = value; }
        }

        /// <summary>
        /// 姓名
        /// </summary>
        string name = "";
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        /// <summary>
        /// 短信内容
        /// </summary>
        string content = "";
        public string Content
        {
            get { return content; }
            set { content = value; }
        }



        /// <summary>
        ///添加一条记录
        /// </summary>
        public int Insert()
        {
            StringBuilder sql = new StringBuilder();
            try
            {
                sql.Append("INSERT into WaitToSendSmsTable");
                sql.Append("(SmsTime,Name,PhoneNumber,SmsOperator,SmsContent,Status,FastSms,NewFlag) ");
                sql.Append(" VALUES(");
                sql.Append("'" + DateTime.Now.ToString() + "'");
                sql.Append(",'" + format(name) + "'");
                sql.Append(",'" + format(telephone) + "'");
                sql.Append(",'" + format("系统管理员") + "'");
                sql.Append(",'" + format(content) + "'");
                sql.Append(",'" + format("待发") + "'");
                sql.Append(",'" + format("0") + "'");
                sql.Append("," + 0);
                sql.Append(")");
            }
            catch (Exception e)
            {
                throw (e);
            }
            return ExecuteSql(sql.ToString());
        }


        /// <summary>
        /// 执行一个查询
        /// </summary>
        /// <param name="sqlString"></param>
        /// <returns></returns>
        public int ExecuteSql(string sqlString)
        {
            int i = 0;
            try
            {
                OleDbConnection sqlConn = new OleDbConnection(connStr);
                OleDbCommand sqlCmd = new OleDbCommand(sqlString, sqlConn);
                sqlConn.Open();
                i = sqlCmd.ExecuteNonQuery();
                sqlConn.Close();
            }
            catch (Exception e)
            {
                throw (e);
            }
            return i;
        }


        /// <summary>
        /// 单引号的处理
        /// </summary>
        public string format(string myString)
        {
            return myString.Replace("'", "''");
        }
    }
}
